package com.easylinkin.linkappapi.jkmonior.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.easylinkin.linkappapi.annotation.CommonOperateLogAnnotate;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.device.entity.Device;
import com.easylinkin.linkappapi.jkmonior.entity.JkMonitorConfig;
import com.easylinkin.linkappapi.jkmonior.entity.vo.JkConfigVo;
import com.easylinkin.linkappapi.jkmonior.entity.vo.JkMonitorConfigVo;
import com.easylinkin.linkappapi.jkmonior.entity.vo.JkMonitorItemConfigVo;
import com.easylinkin.linkappapi.jkmonior.service.JkMonitorConfigService;
import com.easylinkin.linkappapi.jkmonior.service.JkMonitorItemConfigService;
import com.easylinkin.linkappapi.operatelog.constant.LogConstant;
import com.easylinkin.linkappapi.webcammanage.entity.DeviceAttributeStatus;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.List;

/**
 * JkMonitorConfig表控制层
 *
 * @author zhengwen
 * @date 2022/10/28
 */
@Slf4j
@RestController
@RequestMapping("/jkMonitorConfig")
public class JkMonitorConfigController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private JkMonitorConfigService jKMonitorConfigService;

    @Resource
    private JkMonitorItemConfigService jkMonitorItemConfigService;

    /**
     * 分页查询所有数据
     *
     * @param requestModel 查询分页对象
     * @return 所有数据
     */
    @PostMapping("getPage")
    @ApiOperation("查询分页")
    public RestMessage selectPage(@RequestBody RequestModel<JkMonitorConfig> requestModel) {
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        IPage<JkMonitorConfig> record = jKMonitorConfigService.selectPage(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder(record).build();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation("查询单条")
    public RestMessage selectOne(@PathVariable Serializable id) {
        return RestBuilders.successBuilder((this.jKMonitorConfigService.getOneById(id))).build();
    }

    /**
     * 新增数据
     *
     * @param appJkMonitorConfig 实体对象
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation("新增")
    public RestMessage insert(@RequestBody JkMonitorConfig appJkMonitorConfig) {
        return RestBuilders.successBuilder().success((this.jKMonitorConfigService.saveOne(appJkMonitorConfig))).build();
    }

    /**
     * 修改数据
     *
     * @param appJkMonitorConfig 实体对象
     * @return 修改结果
     */
    @PutMapping
    @ApiOperation("修改单条")
    public RestMessage update(@RequestBody JkMonitorConfig appJkMonitorConfig) {
        return RestBuilders.successBuilder().success(this.jKMonitorConfigService.updateOne(appJkMonitorConfig)).build();
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    @ApiOperation("批量删除")
    public RestMessage delete(@RequestParam("idList") List<Long> idList) {
        return RestBuilders.successBuilder().success(this.jKMonitorConfigService.deleteByIds(idList)).build();
    }

    @PostMapping("export")
    @ApiOperation("导出")
    public void export(@RequestBody JkMonitorConfig appJkMonitorConfig, HttpServletRequest request, HttpServletResponse response) {
        jKMonitorConfigService.export(appJkMonitorConfig, request, response);
    }


    /**
     * 查询租户基坑监测基础配置信息
     *
     * @return 统一出参
     */
    @GetMapping("/getJkMonitorBaseConfig")
    @ApiOperation("查询租户基坑监测基础配置信息")
    public RestMessage getJkMonitorBaseConfig() {
        return jKMonitorConfigService.getJkMonitorBaseConfig();
    }

    /**
     * 获取摄像头设备列表
     *
     * @param requestModel 请求入参对象
     * @return 统一出参
     */
    @PostMapping("/getVideoList")
    public RestMessage getVideoList(@RequestBody RequestModel<Device> requestModel) {
        cn.hutool.core.lang.Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        cn.hutool.core.lang.Assert.notNull(requestModel.getPage(), "page 不能为空");

        IPage<DeviceAttributeStatus> deviceVideoList = jKMonitorConfigService.getVideoList(
                requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder().data(deviceVideoList).build();
    }

    /**
     * 保存项目基坑监测基础配置
     *
     * @param jkMonitorConfigVo 实体对象
     * @return 统一出参
     */
    @PostMapping("/saveBaseConfig")
    @ApiOperation("保存项目基坑监测基础配置")
    @CommonOperateLogAnnotate(module = LogConstant.LogModule.DASHBOARD_SETTING, desc = "编辑基坑监测设置")
    public RestMessage saveBaseConfig(@RequestBody JkMonitorConfigVo jkMonitorConfigVo) {
        return jKMonitorConfigService.saveBaseConfig(jkMonitorConfigVo);
    }

    /**
     * 获取监测项点信息
     *
     * @param jkMonitorItemConfigVo 实体对象
     * @return 统一出参
     */
    @PostMapping("/getMonitorItemConfig")
    @ApiOperation("获取监测项点信息")
    public RestMessage getMonitorItemConfig(@RequestBody JkMonitorItemConfigVo jkMonitorItemConfigVo) {
        return jkMonitorItemConfigService.getMonitorItemConfig(jkMonitorItemConfigVo);
    }

    /**
     * 同步获取最新测项、点信息
     *
     * @param jkMonitorItemConfigVo 实体对象
     * @return 统一出参
     */
    @PostMapping("/syncMonitorItem")
    @ApiOperation("同步监测项点信息")
    public RestMessage syncMonitorItem(@RequestBody JkMonitorItemConfigVo jkMonitorItemConfigVo) {
        return jkMonitorItemConfigService.syncMonitorItem(jkMonitorItemConfigVo);
    }

    /**
     * 测项点信息配置保存
     *
     * @param jkConfigVo 实体对象
     * @return 统一出参
     */
    @PostMapping("/saveMonitorItemConfig")
    @ApiOperation("测项点信息配置保存")
    public RestMessage saveMonitorItemConfig(@RequestBody JkConfigVo jkConfigVo) {
        return jkMonitorItemConfigService.saveMonitorItemConfig(jkConfigVo);
    }
}

